package util;

import com.google.common.collect.Lists;
import com.google.errorprone.annotations.Var;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author by zhangql
 * @Classname MapDistanceTest
 * @Description TODO
 * @Date 2021/8/13 17:27
 */

public class MapDistanceTest {

    public static void main(String[] args) {
        //    Map<String, List<LiquidEvent>> mapEvent = events.stream().collect(Collectors.groupingBy(LiquidEvent::getTrucknum));
        List<LiquidEvent> events = new ArrayList<>();
        LiquidEvent liquidEvent = new LiquidEvent(118.442874, 41.784966);
        LiquidEvent liquidEvent1 = new LiquidEvent(121.123569, 29.522673);
        LiquidEvent liquidEvent4 = new LiquidEvent(121.678881, 39.0322);
        events.add(liquidEvent);
        events.add(liquidEvent1);
        events.add(liquidEvent4);

        HashMap<Double, LiquidEvent> minMap = new HashMap<>(1);
        events.forEach(event -> {
            double distance = GeoUtils.distance(event.getEndlng(), event.getEndlat(),121.87105300 , 29.89578700);
            if (ObjectUtils.isEmpty(minMap)) {
                minMap.put(distance, event);
            } else {
                double minKey = minMap.keySet().iterator().next();
                if (distance < minKey) {
                    minMap.remove(minKey);
                    minMap.put(distance, event);
                }
            }
        });
        System.out.println(minMap);
    }
}